package Actor;

import Map.Floor;
import Map.Tile;
import Util.CollisionUtil;
import View.ViewManager;

public class Actor {
	private int x = 0;
	private int y = 0;
	private int bufferX;
	private int bufferY;
	public static final int displacement = 5;
	public Actor() {

	}

	public Actor(int _x, int _y) {
		x = _x;
		y = _y;
	}

	public class UpCommand implements IfaceCharAction {
		@Override
		public void doAcion(Floor map,ViewManager viewManager) {

			setBufferY(y);
			setBufferY(getBufferY() - displacement/displacement);
			if (CollisionUtil.doTryCollisionCheck(new Movable(){
				@Override
				public int getX() {
					return x;
				}
				@Override
				public int getY() {
					return y-displacement/displacement;
				}				
			}, map,this,viewManager))
				Actor.this.rollback();
			else
				Actor.this.commit();

		}
	}

	public class DownCommand implements IfaceCharAction {
		@Override
		public void doAcion(Floor map,ViewManager viewManager) {

			setBufferY(y);
			setBufferY(getBufferY() + displacement/displacement);
			if (CollisionUtil.doTryCollisionCheck(new Movable(){
				@Override
				public int getX() {
					return x;
				}
				@Override
				public int getY() {
					return y+displacement/displacement;
				}				
			}, map,this,viewManager))
				Actor.this.rollback();
			else
				Actor.this.commit();

		}
	}

	public class RightCommand implements IfaceCharAction {
		@Override
		public void doAcion(Floor map,ViewManager viewManager) {

			setBufferX(x);
			setBufferX(getBufferX() + displacement/displacement);
			if (CollisionUtil.doTryCollisionCheck(new Movable(){
				@Override
				public int getX() {
					return x+displacement/displacement;
				}
				@Override
				public int getY() {
					return y;
				}				
			}, map,this,viewManager))
				Actor.this.rollback();
			else
				Actor.this.commit();
		}
	}

	public class LeftCommand implements IfaceCharAction {
		@Override
		public void doAcion(Floor map,ViewManager viewManager) {

			setBufferX(x);
			setBufferX(getBufferX() - displacement/displacement);
			if (CollisionUtil.doTryCollisionCheck(new Movable(){
				@Override
				public int getX() {
					return x-displacement/displacement;
				}
				@Override
				public int getY() {
					return y;
				}				
			}, map,this,viewManager))
				Actor.this.rollback();
			else
				Actor.this.commit();
		}
	}

	public class UpLeftCommand implements IfaceCharAction { // up
		@Override
		public void doAcion(Floor map,ViewManager viewManager) {

			setBufferY(y);
			setBufferY(getBufferY() - displacement);
			setBufferX(x);
			setBufferX(getBufferX() - displacement);
			if (CollisionUtil.doTryCollisionCheck(new Movable(){
				@Override
				public int getX() {
					return x-displacement;
				}
				@Override
				public int getY() {
					return y-displacement;
				}				
			}, map,this,viewManager)){
				Actor.this.rollback();
			} else {
				viewManager.doUpLeftCommand();
				Actor.this.commit();
			}

		}
	}

	public class DownRightCommand implements IfaceCharAction { // down
		@Override
		public void doAcion(Floor map,ViewManager viewManager) {

			setBufferY(y);
			setBufferY(getBufferY() + displacement);
			setBufferX(x);
			setBufferX(getBufferX() + displacement);
			if (CollisionUtil.doTryCollisionCheck(new Movable(){
				@Override
				public int getX() {
					return x+displacement;
				}
				@Override
				public int getY() {
					return y+displacement;
				}				
			}, map,this,viewManager)){
				Actor.this.rollback();
			} else {
				viewManager.doDownRightCommand();
				Actor.this.commit();
			}
		}
	}

	public class UpRightCommand implements IfaceCharAction { // right
		@Override
		public void doAcion(Floor map,ViewManager viewManager) {

			setBufferY(y);
			setBufferY(getBufferY() - displacement);
			setBufferX(x);
			setBufferX(getBufferX() + displacement);
			if (CollisionUtil.doTryCollisionCheck(new Movable(){
				@Override
				public int getX() {
					return x+displacement;
				}
				@Override
				public int getY() {
					return y-displacement;
				}				
			}, map,this,viewManager)){
				Actor.this.rollback();
			} else {
				viewManager.doUpRightCommand();
				Actor.this.commit();
			}
		}
	}

	public class DownLeftCommand implements IfaceCharAction { // left
		@Override
		public void doAcion(Floor map,ViewManager viewManager) {

			setBufferY(y);
			setBufferY(getBufferY() + displacement);
			setBufferX(x);
			setBufferX(getBufferX() - displacement);
			if (CollisionUtil.doTryCollisionCheck(new Movable(){
				@Override
				public int getX() {
					return x-displacement;
				}
				@Override
				public int getY() {
					return y+displacement;
				}				
			}, map,this,viewManager)){
				Actor.this.rollback();
			} else {

				viewManager.doDownLeftCommand();
				Actor.this.commit();
			}
		}
	}

	public int getX() {
		return x;
	}

	public int getY() {
		return y;
	}

	public void setBufferY(int bufferY) {
		this.bufferY = bufferY;
	}

	public int getBufferY() {
		return bufferY;
	}

	public void setBufferX(int bufferX) {
		this.bufferX = bufferX;
	}

	public int getBufferX() {
		return bufferX;
	}

	public void rollback() {
		bufferY = y;
		bufferX = x;
	}

	public void commit() {
		y = bufferY;
		x = bufferX;
	}


}
